//
//  ST_PecSqureFantasticUpdateCell.swift
//  btvideo
//
//  Created by mac on 2020/4/22.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_PecSqureFantasticUpdateCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(left_icon_image_view)
        contentView.addSubview(titleLabel)
        contentView.addSubview(more_data_btn)
        contentView.addSubview(collectionView)
        
        //        contentView.backgroundColor = Color.random
        
        left_icon_image_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(15)
            make.width.height.equalTo(16)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(left_icon_image_view.snp.right).offset(5)
            make.centerY.equalTo(left_icon_image_view)
        }
        
        
        more_data_btn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(left_icon_image_view)
            make.width.equalTo(75)
            make.height.equalTo(25)
        }
        
        
        collectionView.snp.makeConstraints { (make) in
            make.height.equalTo(collection_view_height)
            make.left.right.equalToSuperview()
            make.top.equalTo(more_data_btn.snp.bottom).offset(10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate lazy var left_icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var more_data_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        control.cornerRadius = 12
        control.setTitleForAllStates("查看全部")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(push_more_data_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate let collection_view_height = 179.cgFloat
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
        let controlLayout = UICollectionViewFlowLayout()
        controlLayout.scrollDirection = .horizontal
        controlLayout.itemSize = CGSize.init(width: 120, height: collection_view_height)
        controlLayout.minimumInteritemSpacing = 15
        controlLayout.minimumLineSpacing = 15
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: ST_PecSquareFantasticUpdateChildCell.self)
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.backgroundColor = Color.white
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        //        control.backgroundColor = Color.random
        
        //       control.alwaysBounceVertical = true
        
        return control
    }()
    
    
    //    MARK: - Bind Model
    var model = ST_PeculiaritySquarePersonCellModel(){
        didSet{
            
            left_icon_image_view.image = UIImage.init(named: model.iconImageName)
            
            titleLabel.text = model.title
            
            collectionView.reloadData()
            
        }
    }
}

//MARK: - Event
extension ST_PecSqureFantasticUpdateCell{
    
    @objc func push_more_data_handle(){
        
        let vc = ST_FantasticWaterfallViewController()
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
    }
}


//MARK:- UICollectionViewDelegate
extension ST_PecSqureFantasticUpdateCell:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model.childArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_PecSquareFantasticUpdateChildCell.self, for: indexPath)
        
        cell.child_model = model.childArray[indexPath.row]
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let item = model.childArray[indexPath.row]
        
        let vc = ST_PeculiarityGroupViewController.init(accid: item.accid, groupId: item.group_id.string, peculiarity_id: item.feature_id.string, firstPhotoSize: CGSize.init(width: item.width, height: item.height), remark: item.remark)
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav, animated: true)
        
    }
    
}

//MARK:- Child Cell

class ST_PecSquareFantasticUpdateChildCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(cover_image_view)
        cover_image_view.addSubview(gradient_view)
        
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        gradient_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview()
        }
        
        contentView.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        contentView.addSubview(time_label)
        time_label.snp.makeConstraints { (make) in
            make.left.equalTo(avatar_image_view.snp.right).offset(5)
            make.centerY.equalTo(avatar_image_view)
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.white
        control.numberOfLines = 3
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var gradient_view:ST_FWGradientView = {
       let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor(red: 0.02, green: 0.03, blue: 0.07, alpha: 0).cgColor, UIColor(red: 0.02, green: 0.03, blue: 0.07, alpha: 0.6).cgColor]
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        control.bgLayer.locations = [0.16,1]
        return control
    }()
    
    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 16/2.0
        return control
    }()
    
    
    fileprivate lazy var time_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(white: 1, alpha: 0.7)
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    //    MARK:-
    var child_model = ST_PeculiaritySquarePersonChildModel(){
        didSet{
            
            cover_image_view.pin_setImage(from: child_model.content.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = child_model.remark
            
            avatar_image_view.pin_setImage(from: child_model.avatar.compress_small_image(width: 50).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            time_label.text = child_model.create_time.double.b_t_global_date_string()
            
//            content_label.text =  child_model.own_num.string + "个" + (child_model.type == 1 ? "男生" : "女生")
            
        }
    }
    
}
